#include "StdAfx.h"
#include "Route.h"


RouteStationMgr::RouteStationMgr(void)
{
}

RouteStationMgr::~RouteStationMgr(void)
{
}

void RouteStationMgr::add(Station *sn)
{
	_stations.push_back(sn);
}

void RouteStationMgr::del(Station *sn)
{
	_stations.remove(sn);
}

void RouteStationMgr::clear()
{
	_stations.clear();
}

Station *RouteStationMgr::get(size_t index)
{
	std::list<Station *>::iterator it = _stations.begin();
	std::advance(it, index);

	if (it == _stations.end())
	{
		return NULL;
	}

	return *it;
}

//RouteBusMgr
RouteBusMgr::RouteBusMgr(void)
{
}

RouteBusMgr::~RouteBusMgr(void)
{
}

void RouteBusMgr::add(Bus *bs)
{
	_buses.push_back(bs);
}

void RouteBusMgr::del(Bus *bs)
{
	_buses.remove(bs);
}

void RouteBusMgr::clear()
{
	_buses.clear();
}

Bus *RouteBusMgr::get(size_t index)
{
	std::list<Bus *>::iterator it = _buses.begin();
	std::advance(it, index);

	if (it == _buses.end())
	{
		return NULL;
	}

	return *it;
}

//Route
Route::Route(void)
{

}

Route::~Route(void)
{

}

Database::RouteRow *Route::getDB()
{
	return &_db;
}

void Route::setDB(const Database::RouteRow &rr)
{
	_db = rr;
}

RouteBusMgr *Route::busMgr()
{
	return &_busMgr;
}

//BusMgr
RouteMgr::RouteMgr()
{
}

RouteMgr::~RouteMgr()
{
	destory();
}

Route *RouteMgr::create()
{
	Route *re = new Route;

	_routes.push_back(re);

	for (std::list<IRouteMgrObserver *>::iterator it = _observers.begin(); it != _observers.end(); it++)
	{
		IRouteMgrObserver *rmo = *it;

		rmo->onCreate(this, re);
	}

	return re;
}

void RouteMgr::destory(Route *re)
{
	_routes.remove(re);

	delete re;

	for (std::list<IRouteMgrObserver *>::iterator it = _observers.begin(); it != _observers.end(); it++)
	{
		IRouteMgrObserver *rmo = *it;

		rmo->onDestory(this, re);
	}
}

void RouteMgr::destory()
{
	while (_routes.size() != 0)
	{
		destory(_routes.front());
	}
}

Route *RouteMgr::get(size_t index)
{
	std::list<Route *>::iterator it = _routes.begin();
	std::advance(it, index);

	if (it == _routes.end())
	{
		return NULL;
	}

	return *it;
}

void RouteMgr::addObserver(IRouteMgrObserver *rmo)
{
	_observers.push_back(rmo);
}

void RouteMgr::delObserver(IRouteMgrObserver *rmo)
{
	_observers.remove(rmo);
}